﻿using System;
using System.Configuration;
using System.Runtime.Caching;


namespace FikriKibar.Common.Helpers
{
    public static class CacheHelper
    {

        private static int CacheTimeOut => Convert.ToInt32(ConfigurationManager.AppSettings["CacheTimeOut"]);

        static readonly ObjectCache Cache = MemoryCache.Default;

        static readonly CacheItemPolicy Policy = new CacheItemPolicy
        {
            AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(CacheTimeOut)
        };

        public static void Add(string key, object value)
        {
            var existCache = Get(key);
            if (existCache != null)
            {
                return;
            }
            if (value == null)
            {
                throw new ArgumentNullException(key);
            }
            Cache.Add(key, value, Policy);
        }

        public static void AddForce(string key, object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(key);
            }
            Cache.Add(key, value, Policy);
        }



        public static object Get(string key)
        {
            return Cache.Get(key);
        }

        public static void Delete(string key)
        {
            Cache.Remove(key);
        }

    }
}
